// 递归数组 返回对应id的数组元素对象
type Nullable<T> = T | null

// 使用泛型确保元素类型灵活性，并约束元素必须有id字段
export const recursion = <T extends { id: number }>(
	list: Array<T>,
	id: number,
	fields: Array<string>
) => {
	for (const item of list) {
		if (item.id === id) {
			return item
		}
		for (const field of fields) {
			const nestedList = item[field] as unknown as Array<T> // 类型断言，确保可以继续递归
			if (Array.isArray(nestedList) && nestedList.length > 0) {
				const result = recursion(nestedList, id, fields)
				if (result !== null) {
					return result
				}
			}
		}
	}
	return null
}
